package lift_simulator;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTable;
import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import javax.swing.JPanel;

public class Window {

	private JFrame frame;
	private JTable table;
	int columnSize;
	Object[][] model;
	DefaultTableModel ret;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Building build = new Building(2, 5, 10, new LiftManagerProblem1(),null,null);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Window(Building building) {
		initialize(building);
		this.frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	@SuppressWarnings("serial")
	private void initialize(Building building) {
		frame = new JFrame();
		frame.setResizable(false);
		//frame.setBounds(100, 100, 499, 360);
		frame.setBounds(100, 100, 1000, 360);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		table = new JTable();
		table.setRowSelectionAllowed(false);
		table.setEnabled(false);
		table.setModel(getModel(building));
		//table.getColumnModel().getColumn(columnSize-1).setPreferredWidth(205);
		JScrollPane scrollPane = new JScrollPane(table);
		frame.getContentPane().add(scrollPane, BorderLayout.NORTH);
	}
	/**
	 * 初期化の際に利用するメソッドです。
	 * @param building
	 * @return
	 */
	public DefaultTableModel getModel(Building building){
		String columnName[] = new String[building.getLifts().size()+2];
		columnSize=columnName.length;
		columnName[0]="floor";
		for(int i=0;i<building.getLifts().size();i++)
			columnName[i+1]="lift"+(i+1);
		columnName[columnName.length-1]="tasks";

		model = new Object[building.getFloorSize()*2-1][columnName.length];
		for(int i=0;i<model.length;i++){
			model[i] = new Object[columnName.length];
			Double floorName = new Double(building.getFloorSize()-(i*0.5));
			double fn = floorName.doubleValue();
			if((fn*10)%10!=5)
				model[i][0] = floorName;
			else
				model[i][0] = null;
			for(int j=0;j<building.getLifts().size();j++){
				Lift lift = building.getLifts().get(j);
				if(lift.getCurrentFloor()==fn)
					model[i][j+1] = lift.getTasks().toString();
				else
					model[i][j+1] = null;
			}
			if((fn*10)%10!=5){
				int fnInt = (int)fn;
				model[i][model[i].length-1]=building.getFloor(fnInt).getTasks(true).toString();
			}else{
				model[i][model[i].length-1]=null;
			}
		}
		final ArrayList<Class> column = new ArrayList<Class>();
		column.add(Double.class);
		for(int i=0;i<building.getLifts().size();i++)
			column.add(String.class);
		column.add(String.class);
		ret = new DefaultTableModel(model,columnName){
			Class[] columnTypes = column.toArray(new Class[]{});
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
		};
		return ret;
	}
	/**
	 * 画面を更新します。
	 * @param building
	 */
	public void updateModel(Building building){
		//table.setModel(getModel(building));
		for(int i=0;i<model.length;i++){
			Double floorName = new Double(building.getFloorSize()-(i*0.5));
			double fn = floorName.doubleValue();
			for(int j=0;j<building.getLifts().size();j++){
				Lift lift = building.getLifts().get(j);
				if(lift.getCurrentFloor()==fn)
					ret.setValueAt(lift.getTasks().toString(), i, j+1);
				else
					ret.setValueAt(null, i, j+1);
			}
			if((fn*10)%10!=5){
				int fnInt = (int)fn;
				ret.setValueAt(building.getFloor(fnInt).getTasks(true).toString(), i, model[i].length-1);
			}else{
				ret.setValueAt(null, i, model[i].length-1);
			}
		}
	}
}
